#pragma once
#include"Comm.hpp"
#include <cstdarg>
// 日志是有日志级别的
#define DEBUG   0
#define NORMAL  1
#define WARNING 2
#define ERROR   3
#define FATAL   4

const char *gLevelMap[] = {
    "DEBUG",
    "NORMAL",
    "WARNING",
    "ERROR",
    "FATAL"
};

#define LOGFILE "./selectServer.log"

// 完整的日志功能，至少: 日志等级 时间 支持用户自定义(日志内容, 文件行，文件名)
void logMessage(int level, const char *format, ...)
{
#ifndef DEBUG_SHOW
    if(level== DEBUG) return;
#endif
    char stdBuffer[1024]; //标准部分
    time_t timestamp = time(nullptr);//获取时间戳
    struct tm *t = gmtime(&timestamp);//时间戳转化为UTC
    t->tm_hour+=8;//转北京时间要加8
    std::string myFormat = "%Y-%m-%d %H:%M:%S";
    char mytime[30]={0};
    strftime(mytime, sizeof(mytime), myFormat.c_str(), t);//把时间戳按指定的格式转为字符串

    snprintf(stdBuffer, sizeof stdBuffer, "[%s] [%s] ", gLevelMap[level], mytime);

    char logBuffer[1024]; //自定义部分
    va_list args;
    va_start(args, format);
    // vprintf(format, args);
    vsnprintf(logBuffer, sizeof logBuffer, format, args);
    va_end(args);

    //FILE *fp = fopen(LOGFILE, "a");
    printf("%s%s\n", stdBuffer, logBuffer);
    //fprintf(fp, "%s%s\n", stdBuffer, logBuffer);
    //fclose(fp);
}